package com.sea.app.module.identity.process.manager;

import com.sea.app.module.identity.domain.Role;
import com.sea.app.module.identity.domain.User;
import com.sea.app.module.identity.domain.repository.RoleRepository;
import com.sea.app.module.identity.domain.repository.UserRepository;
import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.GroupEntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 31417 on 2017/4/27.
 */
@Service
public class ProcessGroupEntityManager extends GroupEntityManager {

    @Autowired
    RoleRepository roleRepository;
    @Autowired
    UserRepository userRepository;


    @Override
    public List<Group> findGroupsByUser(String userCode) {
        User user = userRepository.findByNo(userCode);
        List<Role> roles = user.getRoles();
        roles.stream().map(role ->{
            Group group = new GroupEntity();
            group.setId(role.getId().toString());
            group.setName(role.getCode());
            group.setType(role.getType().getCode());
            return group;
        } ).collect(Collectors.toList());
        return super.findGroupsByUser(userCode);
    }

}
